﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XCommon.Types;

namespace XWeb.Code
{
    public enum DBTableCacheKeys : short
    {
        tbGroups = 1,
        tbTodayList = 2,
    }

    public enum EJsonCacheKeys : byte
    {
        tbGroups = 100,           // 导航条
    }

    public class XCacheManager : XCommon.Code.DO<XCacheManager>
    {
        private static int mMaxCacheCount = 300;
        private Dictionary<EJsonCacheKeys, string> mJsonCaches;                         // json 一般保存比较不会更新而且键值固定的数据
        private Dictionary<int, Dictionary<byte, string>> mGroupPageList;               // 每个导航分类下的数据,第一个Key是导航分类ID，第二个Key是页码,从0开始
        private Dictionary<ushort, string> mPhotoDetail;                                // photodetail 里面的数据,key 等于详细列表的主键ID (ushort 16位可能不够用..不过如有那个时候我已经发达了)

        private static object mLockPhotoDetail = new object();

        public XCacheManager()
        {
            mJsonCaches = new Dictionary<EJsonCacheKeys, string>(mMaxCacheCount);
            mGroupPageList = new Dictionary<int, Dictionary<byte, string>>();
            mPhotoDetail = new Dictionary<ushort, string>();
        }

        public string GetPhotoItemJson(ushort itemid)
        {
            return mPhotoDetail.ContainsKey(itemid) ? mPhotoDetail[itemid] : string.Empty;
        }

        public void SetPhotoItemJson(ushort itemid, string json)
        {
            lock (mLockPhotoDetail)
            {
                if (mPhotoDetail.ContainsKey(itemid))
                {
                    mPhotoDetail[itemid] = json;
                }
                else
                {
                    mPhotoDetail.Add(itemid, json);
                }
            }
        }

        public string GetPageJson(int groupid, byte pageindex)
        {
            if (mGroupPageList.ContainsKey(groupid))
            {
                if (mGroupPageList[groupid].ContainsKey(pageindex))
                {
                    return mGroupPageList[groupid][pageindex];
                }
            }
            return string.Empty;
        }

        public void SetPageJson(int groupid, byte pageindex, string jsonValue)
        {
            if (mGroupPageList.ContainsKey(groupid))
            {
                if (mGroupPageList[groupid].ContainsKey(pageindex))
                {
                    mGroupPageList[groupid][pageindex] = jsonValue;
                }
                else
                {
                    mGroupPageList[groupid].Add(pageindex, jsonValue);
                }
            }
            else
            {
                Dictionary<byte, string> tempDic = new Dictionary<byte, string>();
                tempDic.Add(pageindex, jsonValue);
                mGroupPageList.Add(groupid, tempDic);
            }
        }

        private int mtbGroupsCount = -1;
        /// <summary>
        /// 导航条数据条数 ,导航数据有发生改变的话,记得更新这个值
        /// </summary>
        public int TbGroupsCount
        {
            set { mtbGroupsCount = value; }
            get { return mtbGroupsCount; }
        }


        public string GetJson(EJsonCacheKeys key)
        {
            return mJsonCaches.ContainsKey(key) ? mJsonCaches[key] : "";
        }

        public void SetJson(EJsonCacheKeys key, string json)
        {
            if (string.IsNullOrEmpty(json)) return;
            if (mJsonCaches.ContainsKey(key))
            {
                mJsonCaches[key] = json;
            }
            else
            {
                mJsonCaches.Add(key, json);
            }
        }

        public void ClearJson(EJsonCacheKeys key)
        {
            if (mJsonCaches.ContainsKey(key))
            {
                mJsonCaches.Remove(key);
            }
        }

        public void ClearAllJson()
        {
            mJsonCaches = new Dictionary<EJsonCacheKeys, string>();
        }
    }

    public class XCache : XCommon.Code.DO<XCache>
    {
        private static int mMaxCacheCount = 150;

        // 分类导航缓存
        private Dictionary<DBTableCacheKeys, List<Ps>> _dbGroupNavTableCache;

        // 图片子项缓存 最多缓存 mMaxCacheCount 个
        private Dictionary<int, List<List<Ps>>> _photodetailCache;

        // 每个分类的下 第一页缓存
        //  KEY1 = GROUPID, KEY2 = PageIndex
        private Dictionary<int, Dictionary<int, List<Ps>>> _listTableCache;

        public XCache()
        {
            _photodetailCache = new Dictionary<int, List<List<Ps>>>(mMaxCacheCount);
            _listTableCache = new Dictionary<int, Dictionary<int, List<Ps>>>();
            _dbGroupNavTableCache = new Dictionary<DBTableCacheKeys, List<Ps>>();
        }

        public List<Ps> GetListTableCache(int groupid, int ipageindex)
        {
            if (_listTableCache.ContainsKey(groupid) && _listTableCache[groupid].ContainsKey(ipageindex))
            {
                return _listTableCache[groupid][ipageindex];
            }
            return null;
        }

        public void AddListTableCache(int groupid, int ipageindex, List<Ps> list)
        {
            if (list == null || list.Count <= 0) return;
            if (_listTableCache.ContainsKey(groupid))
            {
                if (_listTableCache[groupid].ContainsKey(ipageindex))
                {
                    _listTableCache[groupid][ipageindex] = list;
                }
                else
                {
                    _listTableCache[groupid].Add(ipageindex, list);
                }
            }
            else
            {
                Dictionary<int, List<Ps>> dic = new Dictionary<int, List<Ps>>();
                dic.Add(ipageindex, list);
                _listTableCache.Add(groupid, dic);
            }
        }

        public List<Ps> GetTableCache(DBTableCacheKeys key)
        {
            if (!_dbGroupNavTableCache.ContainsKey(key)) return null;
            return _dbGroupNavTableCache[key];
        }

        public void AddTableCache(DBTableCacheKeys key, List<Ps> value)
        {
            if (!_dbGroupNavTableCache.ContainsKey(key))
            {
                _dbGroupNavTableCache[key] = value;
            }
            else
            {
                _dbGroupNavTableCache.Add(key, value);
            }
        }

        public void ClearTableCache()
        {
            _dbGroupNavTableCache = new Dictionary<DBTableCacheKeys, List<Ps>>();
        }

        public static object mLockPhotoDetail = new object();

        public List<List<Ps>> GetPhotoDetailCache(int itemid)
        {
            if (_photodetailCache.ContainsKey(itemid))
                return _photodetailCache[itemid];
            return null;
        }

        public void AddPhotoDetailCache(int itemid, List<List<Ps>> values)
        {
            lock (mLockPhotoDetail)
            {
                if (this._photodetailCache.ContainsKey(itemid))
                {
                    _photodetailCache[itemid] = values;
                }
                else
                {
                    if (_photodetailCache.Count >= mMaxCacheCount)
                    {
                        int removecount = 10;
                        int[] keys = new int[removecount];
                        int j = 0;
                        foreach (var item in _photodetailCache)
                        {
                            if (j < removecount)
                            {
                                keys[j] = item.Key;
                                j++;
                            }
                            break;
                        }
                        foreach (int i in keys)
                        {
                            _photodetailCache.Remove(i);
                        }
                    }
                    _photodetailCache.Add(itemid, values);
                }
            }
        }
    }
}